package com.jeesite.modules.fl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.entity.User;

public class Utils {
	
	public static List<String> getPeriods(String date, String startTime, String endTime, Integer serviceTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date startDate = DateUtils.parseDate(date);
		boolean isToday = startDate.before(new Date());
		List<String> timePeriods = new ArrayList<String>();
		int totalTime = new Double((new Double(endTime) -  new Double(startTime))*60).intValue();
		int i = 0;
		for(;i<totalTime / serviceTime;i++){
			
			String stateTime = toTime(new Double(new Double(startTime)*60 + serviceTime*i).intValue());
			boolean show = true;			
			if(isToday){
				Date state = new Date();
				try {
					state = sdf.parse(date + " " + stateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(state.before(new Date())){
					show = false;
				}
			}
			if(show){
				timePeriods.add(stateTime + "-" + toTime(new Double(new Double(startTime)*60 + serviceTime*(i+1)).intValue()));
			}
		}
		if(totalTime % serviceTime != 0){
			String stateTime = toTime(new Double(new Double(startTime)*60 + serviceTime*i).intValue());
			boolean show = true;			
			if(isToday){
				Date state = new Date();
				try {
					state = sdf.parse(date + " " + stateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(state.before(new Date())){
					show = false;
				}
			}
			if(show){
				timePeriods.add(stateTime + "-" + toTime(new Double(new Double(startTime)*60 + serviceTime*(i+1)).intValue()));
			}
		}
		return timePeriods;
	}
	
	public static boolean isBussinenssDay(String date, String businessHours){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.parseDate(date));
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		String day = toWeek(week);
		return businessHours.contains(day);
	}
	
	public static Map<String, String> getBussinenssHours(String week, String businessHours){
		Map<String, String> map = new HashMap<String, String>();
		JSONArray array = JSONArray.fromObject(businessHours);
		JSONObject object;
		for (int i = 0; i < array.size(); i++) {
			object = (JSONObject)array.get(i);
			if(object.getString("name").equals(week)){
				map.put("amStart", object.getString("amStart"));
				map.put("amEnd", object.getString("amEnd"));
				map.put("pmStart", object.getString("pmStart"));
				map.put("pmEnd", object.getString("pmEnd"));
				return map;
			}
		}
		return map;
	}
	
	public static boolean IsRoleByRoleId(User user, String roleId){
		List<Role> roleUser = user.getRoleList();
		for(Role r : roleUser){
			if(r.getId().equals(roleId)){
				return true;
			}
		}
		return false;
	}
	
	private static String toTime(int minutes){
		return (minutes/60<10? "0" +minutes/60 :  minutes/60)+ ":" + (minutes%60<10?"0"+minutes%60:minutes%60);
	}
	
	public static void main(String[] args) throws Exception{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtils.parseDate("2019-06-18"));
		String week = toWeek(calendar.get(Calendar.DAY_OF_WEEK));
		System.out.println(week);
		System.out.println(Utils.getPeriods("2019-06-18", "12", "19", 20));
//		System.out.println(Utils.toBusinessString("1,3,5,6,7"));
//		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
//		System.out.println(Utils.checkTime(sdf2.parse("2019-01-18"), "10:30-10:50"));
//		System.out.println(Utils.isBussinenssDay("2019-01-19", "1,2,3,4,5,6"));
	}
	
	public static boolean checkTime(Date date, String timePeriod){
		boolean isToday = date.before(new Date());
		if(isToday){
			String startTime = timePeriod.substring(0, timePeriod.indexOf("-"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			Date state = new Date();
			try {
				state = sdf.parse(sdf2.format(date) + " " + startTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return DateUtils.pastHour(state)<=-2;
		}else{
			return true;
		}
		
		
	}
	
	public static String toBusinessString(String businessHours){
		StringBuilder sb = new StringBuilder();
		if(businessHours == null || businessHours.isEmpty()){
			return "无";
		}
		String[] days = businessHours.split(",");
		int i=0;
		boolean now = false;
		boolean old = false;
		boolean old2 = false;
		int day;
		for(;i<days.length;i++){
			try{
				day = Integer.parseInt(days[i]);
			}catch(Exception e){
				return "无";
			}
			if(i==0){
				sb.append(toString(day));
			}else{
				now = check(new Integer(days[i-1]),day);
				if(now){
					if(i==days.length-1){
						if(old){
							sb.append("至").append(toString(day));
						}else{
							sb.append("、").append(toString(day));
						}
					}
				}else{
					if(old){
						if(old2){
							sb.append("至").append(toString(new Integer(days[i-1]))).append("、").append(toString(day));
						}else{
							sb.append("、").append(toString(new Integer(days[i-1]))).append("、").append(toString(day));
						}
					}else{
						sb.append("、").append(toString(day));
					}
				}
			}
			old2 = old;
			old = now;
		}
		return sb.toString();
	}
	
	private static boolean check(int i, int j){
		return j-i==1;
	}
	
	public static String toWeek(int i){
		int day = i-1;
		if(day == 0){
			return "7";
		}else{
			return ""+day;
		}
	}
	
	private static String toString(int i){
		if(i==1){
			return "周一";
		}if(i==2){
			return "周二";
		}if(i==3){
			return "周三";
		}if(i==4){
			return "周四";
		}if(i==5){
			return "周五";
		}if(i==6){
			return "周六";
		}else{
			return "周日";
		}
	}

}
